package com.tbit.main.controller;

import cn.hutool.core.bean.BeanUtil;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.UpKeepConfig;
import com.tbit.main.pojo.VO.UpKeepConfigReqVO;
import com.tbit.main.service.UpKeepConfigService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

/**
 * 保养配置
 * @author yao
 */
@RestController
@RequestMapping("/upKeepConfig")
public class UpKeepConfigController {

    @Resource
    private UpKeepConfigService upKeepConfigService;

    // 保养配置
    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.UN_KEEP_CONFIG_EDIT)
    public JsonResult<?> edit(@Valid UpKeepConfigReqVO reqVO) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();

        UpKeepConfig upKeepConfig = BeanUtil.copyProperties(reqVO, UpKeepConfig.class);

        Date now = new Date();
        upKeepConfig.setCreator(loginUser.getAccountUserId().toString());
        upKeepConfig.setCreateTime(now);
        upKeepConfig.setUpdater(loginUser.getAccountUserId().toString());
        upKeepConfig.setUpdateTime(now);

        upKeepConfigService.createOrUpdate(upKeepConfig);

        return JsonResult.succ();
    }


    /**
     * 获取
     */
    @RequestMapping("/getByAccount")
    public JsonResult<UpKeepConfig> getByAccount(@Valid UpKeepConfigReqVO reqVO) {

        UpKeepConfig upKeepConfig = upKeepConfigService.getByAccountId(reqVO.getAccountId());

        return JsonResult.succ(upKeepConfig);
    }
}
